﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using iShoppingDTO;

namespace iShoppingDAO
{
    public class CommentDAO
    {
        public static bool InsertComment(CommentDTO commentDto)
        {
            bool res = false;
            try
            {
                string sql = "INSERT INTO Comment(MaComment,NoiDungComment,NgayComment,MaTaiKhoan,MaSanPham) VALUES(@MaComment,@NoiDungComment,@NgayComment,@MaTaiKhoan,@MaSanPham)";
                List<SqlParameter> parameters = new List<SqlParameter>();

                SqlParameter[] par = new SqlParameter[5];
                par[0] = new SqlParameter("@MaComment", SqlDbType.NVarChar);
                par[0].Value = commentDto.MaCommment.ToString();
                parameters.Add(par[0]);
                par[1] = new SqlParameter("@NoiDungComment", SqlDbType.NVarChar);
                par[1].Value = commentDto.NoiDungComment;
                parameters.Add(par[1]);
                par[2] = new SqlParameter("@NgayComment", SqlDbType.DateTime);
                par[2].Value = commentDto.NgayComment;
                parameters.Add(par[2]);
                par[3] = new SqlParameter("@MaTaiKhoan", SqlDbType.NVarChar);
                par[3].Value = commentDto.MaTaiKhoan;
                parameters.Add(par[3]);
                par[4] = new SqlParameter("@MaSanPham", SqlDbType.NVarChar);
                par[4].Value = commentDto.MaSanPham;
                parameters.Add(par[4]);

                res = DataProvider.ExecuteNoneQuery(sql, parameters);

            }
            catch (Exception)
            {
                res = false;
            }
            return res;

        }

        public static List<CommentDTO> GetCommentOfSP(string maSP)
        {
            List<CommentDTO> commentDTOs = new List<CommentDTO>();
            try
            {

                String sql = "SELECT * FROM Comment cm, TaiKhoan tk WHERE cm.MaTaiKhoan = tk.MaTaiKhoan AND MaSanPham = '" + maSP + "'  ORDER BY NgayComment DESC";
                DataTable dt = DataProvider.ExecuteQuery(sql);

                int i, n = dt.Rows.Count;
                for (i = 0; i < n; i++)
                {
                    CommentDTO sp = new CommentDTO();
                    sp.MaCommment = long.Parse(dt.Rows[i]["MaComment"].ToString());
                    sp.NoiDungComment = dt.Rows[i]["NoiDungComment"].ToString();
                    sp.NgayComment = DateTime.Parse(dt.Rows[i]["NgayComment"].ToString());
                    sp.MaTaiKhoan = dt.Rows[i]["MaTaiKhoan"].ToString().Trim();
                    sp.MaSanPham = dt.Rows[i]["MaSanPham"].ToString();
                    sp.TenTaiKhoan = dt.Rows[i]["TenTaiKhoan"].ToString();
                    sp.Avatar = dt.Rows[i]["Avatar"].ToString();
                    commentDTOs.Add(sp);
                }


            }
            catch (Exception ex)
            {
                commentDTOs = new List<CommentDTO>();
            }
            return commentDTOs; ;
        }

        public static DataTable GetComment(string maSP)
        {
            DataTable dataTable = new DataTable();
            try
            {

                String sql = "SELECT * FROM Comment WHERE MaSanPham = '" + maSP + "'";
                dataTable = DataProvider.ExecuteQuery(sql);


            }
            catch (Exception ex)
            {
                dataTable = new DataTable();
            }
            return dataTable; ;
        }

        public static bool UpdateComment(string noidung)
        {
            bool res = false;
            try
            {
                string sql = "UPDATE Comment SET NoiDungComment = @NoiDungComment WHERE MaSanPham = @MaSanPham";
                List<SqlParameter> parameters = new List<SqlParameter>();

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@NoiDungComment", SqlDbType.NVarChar);
                par[0].Value = noidung;
                parameters.Add(par[0]);
                res = DataProvider.ExecuteNoneQuery(sql, parameters);

            }
            catch (Exception)
            {
                res = false;
            }
            return res;
        }

        public static bool DeleteComment(string macomment)
        {
            bool kq = false;
            try
            {

                String sql = "Delete Comment where MaComment = " + macomment;
                DataProvider.ExecuteNoneQuery(sql);
                kq = true;
            }
            catch (Exception ex)
            {
            }
            return kq;
        }
    }
}
